﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsPlatform.PermissionManagement.IPermissionManagement;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Query.PermissionManagement.PlatformInfo;
using SportsPlatform.Model.PermissionManagement;
using SportsPlatform.Query.PermissionManagement.BaseQuery;
using SportsPlatform.Dto.PermissionManagement.PlatformInfo;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Http;
using Abp.Application.Services;

namespace SportsPlatform.PermissionManagement.PermissionManagement
{
    /// <summary>
    /// 平台信息表
    /// </summary>
    [RemoteService(false)]  //隐藏自动生成的WebApi
    public class PlatformInfoService : SportsPlatformAppServiceBase, IPlatformInfoService
    {

        //public ISqlSugarClient _sugar;
        //public IObjectMapper _mapper;
        //public DataValidation _dataValidation;
        //public PlatformInfoService(ISqlSugarClient sugar, IObjectMapper mapper)
        //{
        //    _sugar = sugar;
        //    _mapper = mapper;
        //    _dataValidation = new DataValidation(sugar);
        //}
        private readonly IRepository<PlatformInfo, Guid> _platforminfoRepository;

        private readonly IRepository<UserInfo, Guid> _userinfoRepository;

        private IHttpContextAccessor _httcontext;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="platforminfoRepository"></param>
        /// <param name="userinfoRepository"></param>
        /// <param name="httpContextAccessor"></param>
        public PlatformInfoService(IRepository<PlatformInfo, Guid> platforminfoRepository, IRepository<UserInfo, Guid> userinfoRepository, IHttpContextAccessor context) : base(context)
        {
            _platforminfoRepository = platforminfoRepository;
            _userinfoRepository = userinfoRepository;
        }

        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="addPlatformInfoQuery"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> AddPlatformInfo(AddPlatformInfoQuery addPlatformInfoQuery)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                if (addPlatformInfoQuery.CreateUserId == Guid.Empty)
                {
                    throw new Exception("CreateUserId不能为空！");
                }
                var userinfo = await _userinfoRepository.FirstOrDefaultAsync(info => info.Id == addPlatformInfoQuery.CreateUserId);
                if (userinfo == null)
                {
                    throw new Exception("CreateUserId查询不到用户信息！");
                }

                PlatformInfo platformInfo = ObjectMapper.Map<PlatformInfo>(addPlatformInfoQuery);
                platformInfo.States = true;
                platformInfo.CreateTime = DateTime.Now;
               var platformid=  _platforminfoRepository.InsertAndGetId(platformInfo);
                if (platformid != Guid.Empty)
                {
                    basePage.IsSuccess = true;
                    basePage.Code = HttpResultCode.Success;
                    basePage.Message = "新增成功！";
                }
                else {
                    basePage.IsSuccess = false;
                    basePage.Code = HttpResultCode.SqlError;
                    basePage.Message = "执行sql失败！";
                }
            }
            catch (Exception e)
            {
                basePage.IsSuccess = false;
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }

        /// <summary>
        /// 根据Id删除信息
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> DeletePlatformInfo(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                //int ret = await _sugar.Updateable<PlatformInfo>()
                //    .Where(info => info.Id == deleteOrQueryModel.Id)
                //    .SetColumns(info => info.IsDeleted == true)
                //    .SetColumns(info => info.DeleteTime == DateTime.Now)
                //    .SetColumns(info => info.DeleteUserId == deleteOrQueryModel.DeleteUserId)
                //    .ExecuteCommandAsync();
                //basePage.Code = ret > 0 ? HttpResultCode.Success : HttpResultCode.SqlError;
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }

        /// <summary>
        /// 根据Id获取信息
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        public async Task<ResultDto<GetPlatformInfoDto>> GetPlatformInfo(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            ResultDto<GetPlatformInfoDto> pageData = new ResultDto<GetPlatformInfoDto>();
            pageData.Code = HttpResultCode.SqlError;
            try
            {
                //PlatformInfo platformInfo = await _sugar.Queryable<PlatformInfo>().SingleAsync(info => info.Id == deleteOrQueryModel.Id);
                //if (platformInfo != null)
                //{
                //    pageData.Code = HttpResultCode.Success;
                //    pageData.Data = _mapper.Map<GetPlatformInfoDto>(platformInfo);
                //}
            }
            catch (Exception e)
            {
                pageData.Code = HttpResultCode.Error;
                pageData.Message = e.Message.ToString();
            }
            return pageData;
        }

        /// <summary>
        /// 获取平台信息Llis
        /// </summary>
        /// <param name="getPlatformInfoQuery"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultListDto<GetPlatformInfoDto>> GetPlatformInfoList(GetPlatformInfoQuery getPlatformInfoQuery)
        {
            ResultListDto<GetPlatformInfoDto> resultListDto = new ResultListDto<GetPlatformInfoDto>();
            resultListDto.Code = HttpResultCode.SqlError;
            try
            {
                //var platforminfolist = await _sugar.Queryable<PlatformInfo>()
                //    .WhereIF(getPlatformInfoQuery.Id != null, info => info.Id == getPlatformInfoQuery.Id)
                //    .WhereIF(!string.IsNullOrEmpty(getPlatformInfoQuery.PlatformName), info => info.PlatformName.Equals(getPlatformInfoQuery.PlatformName))
                //    .WhereIF(getPlatformInfoQuery.States != null, info => info.States == getPlatformInfoQuery.States)
                //    .OrderByDescending(info => info.CreateTime)
                //    .ToListAsync();
                //if (platforminfolist != null && platforminfolist.Count > 0)
                //{
                //    resultListDto.Code = HttpResultCode.Success;
                //    resultListDto.Count = platforminfolist.Count;
                //    List<GetPlatformInfoDto> getPlatformInfoDtos = new List<GetPlatformInfoDto>();
                //    platforminfolist.ForEach(pfinfo =>
                //    {
                //        getPlatformInfoDtos.Add(_mapper.Map<GetPlatformInfoDto>(pfinfo));
                //    });
                //    resultListDto.Data = getPlatformInfoDtos;
                //}
            }
            catch (Exception e)
            {
                resultListDto.Code = HttpResultCode.Error;
                resultListDto.Message = e.Message.ToString();
            }
            return resultListDto;
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        /// <param name="isStatesModel"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> IsStatesPlatformInfo(IsStatesdModel isStatesModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                //判断UserId是否有效
                //UserInfoJudge userInfoJudge = new UserInfoJudge();
                //userInfoJudge.Id = isStatesModel.UpdateUserId;
                //if (await _dataValidation.GetUserInfoJudge(userInfoJudge))
                //{
                //    int ret = await _sugar.Updateable<PlatformInfo>()
                //         .Where(info => info.Id == isStatesModel.Id)
                //         .SetColumns(info => info.UpdateTime == DateTime.Now)
                //         .SetColumns(info => info.States == isStatesModel.States)
                //         .SetColumns(info => info.UpdateUserId == isStatesModel.UpdateUserId)
                //         .ExecuteCommandAsync();
                //    basePage.Code = ret > 0 ? HttpResultCode.Success : HttpResultCode.SqlError;
                //}
                //else
                //{
                //    basePage.Code = HttpResultCode.Fail;
                //    basePage.Message = "UserId无效,不存在有效的信息！";
                //}
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="updatePlatformInfoQuery"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> UpdatePlatformInfo(UpdatePlatformInfoQuery updatePlatformInfoQuery)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                //判断UserId是否有效
                //UserInfoJudge userInfoJudge = new UserInfoJudge();
                //userInfoJudge.Id = updatePlatformInfoQuery.UpdateUserId;
                //if (await _dataValidation.GetUserInfoJudge(userInfoJudge))
                //{
                //    int ret = await _sugar.Updateable<PlatformInfo>()
                //        .Where(info => info.Id == updatePlatformInfoQuery.Id)
                //        .SetColumns(info => info.UpdateTime == DateTime.Now)
                //        .SetColumns(info => info.UpdateUserId == updatePlatformInfoQuery.UpdateUserId)
                //        .SetColumnsIF(updatePlatformInfoQuery.States != null, info => info.States == updatePlatformInfoQuery.States)
                //        .SetColumnsIF(!string.IsNullOrEmpty(updatePlatformInfoQuery.PlatformName), info => info.PlatformName == updatePlatformInfoQuery.PlatformName)
                //        .SetColumnsIF(!string.IsNullOrEmpty(updatePlatformInfoQuery.PlatformDescribe), info => info.PlatformDescribe == updatePlatformInfoQuery.PlatformDescribe)
                //        .ExecuteCommandAsync();
                //    basePage.Code = ret > 0 ? HttpResultCode.Success : HttpResultCode.SqlError;
                //}
                //else
                //{
                //    basePage.Code = HttpResultCode.Fail;
                //    basePage.Message = "UserId无效,不存在有效的信息！";
                //}
            }
            catch (Exception e)
            {
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
    }
}
